home *** CD-ROM | disk | FTP | other *** search
- /**************************************************************************
- * showmjb.c *
- * - sample code for reading BGI format images written by PCX2BGI.EXE *
- * Takes .mjb file as command line arg and displays a full screen *
- * or partial-screen image *
- * - marty balash *
- * - 03/02/91 *
- * *
- * If you find any of these utilities useful, mail $15.00 to: *
- * Marty Balash *
- * 2 Pinecrest Dr. *
- * Prospect CT 06712 *
- * *
- * Add EGAVGA.OBJ to project/make file *
- **************************************************************************/
-
- #include <graphics.h>
- #include <alloc.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <string.h>
- #include <io.h>
-
- struct IMAGEHDR{
- char id[8]; /* id - use this for verifying format */
- unsigned size; /* size - bytes to malloc */
- struct palettetype palette; /* palette - for image */
- }hdr;
-
-
- long fileexists();
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- long filelen;
- char filename[50];
-
- if(argc==1){
- printf("SHOWMJB.EXE - By Marty Balash\n");
- printf(" Read a file created by PCX2BGI\n\n");
- printf("*** ERROR: Must Specify .MJB Filename\n");
- exit(255);
- }
- strcpy(filename,argv[1]);
- strupr(filename);
- if(argc==2){
- if(strstr(filename,".MJB")==NULL)
- strcat(filename,".MJB");
- if((filelen=fileexists(filename))==0){
- printf("ERROR: Can't find %s",filename);
- exit(255);
- }
- }
- if(!openegascreen()){
- puts("Can't open EGA screen");
- exit(255);
- }
- if(filelen>100000L) /* whole screen is at least this big */
- readscreen(filename);
- else
- readimage(filename);
- getch();
- closegraph();
- }
-
- /**************************************************************************
- * readimage - arg is filename of graphics file to display *
- * returns 1 if successful, 0 if not *
- **************************************************************************/
- readimage(fn)
- char *fn;
- {
- int filehandle;
- char *buff;
-
- if((filehandle=open(fn,O_RDONLY|O_BINARY))==-1) /* open graphics file */
- return(0);
- read(filehandle,&hdr,sizeof(hdr)); /* read size (to malloc) & palette */
- setallpalette(&hdr.palette); /* set palette to palette from file */
- if((buff=malloc(hdr.size))==NULL) /* alloc mem - unsigned int from file */
- return(0);
- read(filehandle,buff,hdr.size); /* read in the image */
- putimage(50,30,buff,COPY_PUT); /* put the image on the screen */
- close(filehandle);
- free(buff); /* clean up */
- return(1);
- }
-
- /***************************************************************************
- * readscreen - param is filename of graphics file to display *
- * Returns 1 if successful, 0 if not *
- * Renders images on non-visable page, then switches pages *
- * Screen is rendered with 5 separate screen-width images *
- ***************************************************************************/
- readscreen(fn)
- char *fn;
- {
- int filehandle,i;
- char *buff;
-
- if((filehandle=open(fn,O_RDONLY|O_BINARY))==-1) /* open graphics file */
- return(0);
- read(filehandle,&hdr,sizeof(hdr)); /* read size (to malloc) & palette */
- puts("LOADING...");
- setactivepage(1); /* render graphics on other page */
- setallpalette(&hdr.palette); /* set palette to palette from file */
- if((buff=malloc(hdr.size))==NULL) /* alloc mem - unsigned int from file */
- return(0);
- for(i=0;i<350;i+=70){
- read(filehandle,buff,hdr.size); /* read 5 horizonal image "slices" */
- putimage(0,i,buff,COPY_PUT); /* put images on the (other) page */
- }
- free(buff); /* clean up */
- close(filehandle);
- setvisualpage(1); /* image rendered on non-visual page, display it now */
- return(1);
- }
-
- openegascreen() /* open 640x350 16-color EGA screen */
- {
- int driver = EGA;
- int mode = EGAHI;
- int result;
-
- if (registerbgidriver(EGAVGA_driver)<0){
- printf("ERROR: Graphics System\n");
- return(0);
- }
- initgraph(&driver,&mode,"");
- result=graphresult();
- if (result!=grOk) {
- printf("ERROR : %s\n",grapherrormsg(result));
- return(0);
- }
- return(1);
- }
-
- long fileexists(fn)
- char *fn;
- {
- int f;
- long len;
- if((f=open(fn,O_RDONLY))!=-1){
- len=filelength(f);
- read(f,&hdr,sizeof(hdr));
- if(strcmp("PCX2BGI",hdr.id)!=0){ /* should always be "PCX2BGI" */
- printf("ERROR: Incorrect Format or File Corrupt\n");
- close(f);
- exit(255);
- }
- close(f);
- return(len);
- }
- else
- return(0);
- }
-
-